This post is on little tool mimeswacurl for sending an arbitrary file as attachment of a SOAP with Attachments file to a service endpoint.
This is really useful if developing a DataPower service dealing with SOAP with Attachments.
 
On DataPower the so called attachment-manifest gives details of the SOAP document as well as all atachments.
 
Stylesheet manifest.xsl which just outputs var://local/attachment-manifest for demo application below:
$ cat manifest.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
extension-element-prefixes="dp"
>
<xsl:output omit-xml-declaration="yes" />

<xsl:template match="/">
<xsl:copy-of select="dp:variable('var://local/attachment-manifest')" />
</xsl:template>

</xsl:stylesheet>
$
 
 
On DataPower box dp3-l3 port 2051 there is a simple loopback XML FW:
 
mimeswacurl
tool just
 
What you can see here is the output of attachment-manifest extraction service described above pretty printed by tidy tool:
$ mimeswacurl xslt.pdf http://dp3-l3:2051 | tidy -q -xml
<manifest>
<package-headers />
<root-headers>
<header>
<name>Content-Type</name>
<value>application/soap+xml</value>
</header>
<header>
<name>Content-Location</name>
<value>root</value>
</header>
</root-headers>
<media-type>
<value>multipart/related; boundary="epoch-1299951959";
type="text/xml"</value>
<type>multipart</type>
<sub-type>related</sub-type>
</media-type>
<attachments>
<attachment>
<uri>cid:binary</uri>
<size>61806</size>
<header>
<name>Content-Type</name>
<value>application/pdf</value>
</header>
<header>
<name>Content-Transfer-Encoding</name>
<value>binary</value>
</header>
<header>
<name>Content-Id</name>
<value><binary></value>
</header>
</attachment>
</attachments>
</manifest>
 
$
  
As promised the Content-Type of attachment cid:binary (which is xslt.pdf) is application/pdf.
 
And this is the little Java application for detecting the MIME type:
$ cat MimeType.java 
import java.net.FileNameMap;
import java.net.URLConnection;
 
public class MimeType {
public static void main(String args[]) throws Exception {

String mimetype = URLConnection.getFileNameMap().getContentTypeFor(args[0]);
 
System.out.println( (mimetype!=null) ? mimetype : "application/octet-stream" );
}
}
$
 
 
Last, but not least, this is mimeswacurl tool -- a shell script working under Linux or Cygwin on Windows systems:
$ cat mimeswacurl 
#!/bin/bash
 
boundary=epoch-`date +%s`
 
echo "--$boundary
Content-Type: application/soap+xml
Content-Location: root
 
<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">
<env:Body>
<some_soap/>
</env:Body>

</env:Envelope>
--$boundary
Content-Type: `java -classpath ~/bin MimeType $1`
Content-Transfer-Encoding: binary
Content-Id: <binary>
 
" `cat $1` "
--$boundary--
" | curl -s --data-binary @- -H 'Content-Type: multipart/related; type="text/xml"; boundary="'$boundary'"' $2
 
$
 
For download:
manifest.xsl
mimeswacurl
MimeType.java
 
 
Hermann.